package com.lagou;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.config.Config;

import java.util.concurrent.TimeUnit;

public class RedissonDistributedLock {
    private static Redisson redisson;

    static {
        Config config = new Config();
        config.useSingleServer().setAddress("127.0.0.1:6379");
        redisson = (Redisson) Redisson.create(config);
    }

    /**
     * 加锁
     */
    public static void acquire(String lockName) {
        RLock lock = redisson.getLock(lockName);
        lock.lock(5, TimeUnit.SECONDS);
    }

    /**
     * 释放锁
     */
    public static void release(String lockName) {
        RLock lock = redisson.getLock(lockName);
        lock.unlock();
    }
}
